{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e6852cc0",
   "metadata": {},
   "source": [
    "* 本学习记录人：林文轩\n",
    "* 学习实践：2021-11-18\n",
    "* week07\n",
    "\n",
    "# 回顾上周内容\n",
    "\n",
    "## 1. 认识列表 ：[value1,value2,value3……]\n",
    "\n",
    "## 2.列表的查询\n",
    "\n",
    "> 1. 索引值进行查询(单个值): index  \n",
    "> 2. 切片进行查询(多个值): slice  \n",
    "\n",
    "## 3. 列表的一般方法\n",
    "\n",
    "> 1. 增加元素  \n",
    ">> 1. append() 增加元素到列表的末尾: L.append(object)  \n",
    ">> 2. insert() 指定位置增加元素: L.insert(index)  \n",
    "> 2. 删除元素  \n",
    ">> 1. remove()按照 值 进行删除: L.remove(value)  \n",
    ">> 2. pop()按照 索引值 进行删除: L.pop(index) 注意：不屑index默认删除列表的最后一个元素  \n",
    "> 3. 修改内容  \n",
    "> 4. 查询内容（参考上述2）  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7f29843f",
   "metadata": {},
   "source": [
    "# 本周内容：\n",
    "## 列表的其他方法\n",
    "## 列表的实践（课本）\n",
    "## 列表的实践（课外）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "806ae37d",
   "metadata": {},
   "outputs": [],
   "source": [
    "names = [\"Mike\",\"Mary\",\"Jan\",\"Jack\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "3676cd2d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Mike', 'Mary', 'Jan', 'Jack']"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "56199921",
   "metadata": {},
   "source": [
    "> 1. clear\n",
    ">> 1. 使用方法：L.clear()\n",
    ">> 2. 结果： remove all items from L"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "2b2c51be",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names.clear()\n",
    "names"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9896faeb",
   "metadata": {},
   "source": [
    "> 2. count()\n",
    ">> 1.使用方法: L.count(value)  \n",
    ">> 2.结果:  查询某个value出现的次数（共现的次数），通常用在计算词频、统计操作当中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "3d3bcd23",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[88, 89, 68, 78, 88, 90, 93, 88]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "分数_list = [88,89,68,78,88,90,93,88]\n",
    "分数_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "44b25ab4",
   "metadata": {},
   "source": [
    "> 3. extend() 扩展\n",
    ">> 1. 使用方法: L.extend(iterable)  \n",
    ">> 2. 结果: iterable 将拼接在 L 的后面（末尾）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "9969f054",
   "metadata": {},
   "outputs": [],
   "source": [
    "分数_list_数媒技术1班 = [88,89,68,78,88,90,93,88]\n",
    "分数_list_数媒技术2班 = [88,78,76,77,88,89,77,66]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "b1447035",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function extend:\n",
      "\n",
      "extend(iterable, /) method of builtins.list instance\n",
      "    Extend list by appending elements from the iterable.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(分数_list.extend)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "25fa2a45",
   "metadata": {},
   "outputs": [],
   "source": [
    "分数_list_数媒技术1班.extend(分数_list_数媒技术2班)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "546113be",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[88, 89, 68, 78, 88, 90, 93, 88, 88, 78, 76, 77, 88, 89, 77, 66]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "分数_list_数媒技术1班"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "9d3a0fc9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[88, 89, 68, 78, 88, 90, 93, 88, 88, 78, 76, 77, 88, 89, 77, 66]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#等价于\n",
    "[88,89,68,78,88,90,93,88] + [88,78,76,77,88,89,77,66]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2627aa00",
   "metadata": {},
   "source": [
    "> 4. **copy()**\n",
    ">> 1. 在列表中，“赋值”不等价于“复制”\n",
    ">> 2. 真正的复制列表的两种常见方法\n",
    ">>> 1. copy\n",
    ">>> 2. 切片[:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "0c391e26",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Jack Alex\n",
      "2990159198000 2990159197552\n",
      "name1 和 name2 拥有不同的数据地址\n"
     ]
    }
   ],
   "source": [
    "name1 = \"Alex\"\n",
    "name2 = name1\n",
    "name1 = \"Jack\"\n",
    "print(name1,name2)\n",
    "print(id(name1),id(name2))\n",
    "print(\"name1 和 name2 拥有不同的数据地址\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b385a1a1",
   "metadata": {},
   "source": [
    "* 赋值有两种情况\n",
    "> 1. 赋值的数据类型是不可变的数据（字符串、数值、元组），分配不同的数据地址  \n",
    "> 2. 赋值的数据类型是可变的数据(列表)非陪同一个地址，指向同一个列表  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "8b145710",
   "metadata": {},
   "outputs": [],
   "source": [
    "first = [1,2,3,4,5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "7eb42250",
   "metadata": {},
   "outputs": [],
   "source": [
    "second = first"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "1c228b64",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "second"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "2e1c2867",
   "metadata": {},
   "outputs": [],
   "source": [
    "first.append(6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "98368444",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5, 6] [1, 2, 3, 4, 5, 6]\n"
     ]
    }
   ],
   "source": [
    "print(first,second)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b53e2ee6",
   "metadata": {},
   "source": [
    "## 列表的复制（方法一）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "bfff7173",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5] [1, 2, 3, 4, 5]\n",
      "2990158827328 2990158642240\n"
     ]
    }
   ],
   "source": [
    "first = [1,2,3,4,5]\n",
    "second = first.copy()\n",
    "\n",
    "print(first,second)\n",
    "print(id(first),id(second))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "2363b7ee",
   "metadata": {},
   "outputs": [],
   "source": [
    "first.append(6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "07e7ac31",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5, 6] [1, 2, 3, 4, 5]\n"
     ]
    }
   ],
   "source": [
    "print(first,second)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1e7d7d85",
   "metadata": {},
   "source": [
    "## 列表的复制(方法二)\n",
    "* 切片.[]现真正意义上的复制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "1bbee6b2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5] [1, 2, 3, 4, 5]\n",
      "2990158900672 2990158907008\n"
     ]
    }
   ],
   "source": [
    "first = [1,2,3,4,5]\n",
    "second = first[:] \n",
    "\n",
    "print(first,second)\n",
    "print(id(first),id(second))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "365462b5",
   "metadata": {},
   "source": [
    "## 思考 copy() 只复制 第一层"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "8a35c75b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, [4, 5, 6]] [1, 2, 3, [4, 5, 6]]\n",
      "2990159255744 2990158900096\n"
     ]
    }
   ],
   "source": [
    "first = [1,2,3,[4,5,6]]\n",
    "second = first.copy()\n",
    "\n",
    "print(first,second)\n",
    "print(id(first),id(second))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "5a0c75f8",
   "metadata": {},
   "outputs": [],
   "source": [
    "first[3].append(7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "ee628c50",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, [4, 5, 6, 7]]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "first"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "a1018a78",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, [4, 5, 6, 7]]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "second"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ac05b035",
   "metadata": {},
   "source": [
    "> 6. sort()  \n",
    ">> 1. reverse = False(默认从小到大）  \n",
    ">> 2. 可修改reverse = Ture(从大到小)  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "3f2cefbe",
   "metadata": {},
   "outputs": [],
   "source": [
    "分数_list_数媒技术1班.sort()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "753886cf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[68, 78, 88, 88, 88, 89, 90, 93]"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "分数_list_数媒技术1班"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "e06d7f1a",
   "metadata": {},
   "outputs": [],
   "source": [
    "分数_list_数媒技术1班.sort(reverse = True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "06fa8b08",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[93, 90, 89, 88, 88, 88, 78, 68]"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "分数_list_数媒技术1班"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "18e9520b",
   "metadata": {},
   "source": [
    "# 列表的实践（课本）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "05b7305b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# vowels元音\n",
    "vowels = ['a','e','i','o','u']\n",
    "\n",
    "word = input()\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "bf6fa77a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n"
     ]
    }
   ],
   "source": [
    "# 目标：把下面的句子改成on tap\n",
    "phrase = \"Don't panic!\"\n",
    "plist = list(phrase) # list()\n",
    "print(plist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "cabd097c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n"
     ]
    }
   ],
   "source": [
    "# 删除 D\n",
    "plist.remove('D')\n",
    "print(plist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "9c448947",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['o', 'n', \"'\", 't', ' ', 'p', 'a']\n"
     ]
    }
   ],
   "source": [
    "#删除末尾4个元素 用pop()删除4次\n",
    "for i in range(4):\n",
    "    plist.pop()\n",
    "print(plist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "9653e5fc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['o', 'n', 't', ' ', 'p', 'a']\n",
      "['o', 'n', 't', ' ', 'a', 'p']\n",
      "['o', 'n', ' ', 't', 'a', 'p']\n",
      "on tap\n"
     ]
    }
   ],
   "source": [
    "# plist.extend([plist.pop(),plist.pop()])\n",
    "# ['o','n','t'] + ['a','p']\n",
    "plist.pop(2)\n",
    "print(plist)\n",
    "plist.insert(4,plist.pop())\n",
    "print(plist)\n",
    "plist.insert(2,plist.pop(3))\n",
    "print(plist)\n",
    "new_phrase = ''.join(plist)\n",
    "print(new_phrase )"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "60e1ac89",
   "metadata": {},
   "source": [
    "# 列表的实践二（课本）\n",
    "\n",
    "* 对比 列表的实践一 结论：\n",
    "> 1. 列表的方法会修改原本的列表，但是**列表的切片**不改变原列表\n",
    "> 2. 使用列表的方法是，需考虑是否需要先复制列表\n",
    "> 3. 思考：如何对比代码的执行效率？代码执行后的时间 - 代码执行前的时间"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "f5b22dcb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1637830981.807146"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import time\n",
    "time.time()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "5fe38790",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "on tap\n",
      "0.0009982585906982422\n"
     ]
    }
   ],
   "source": [
    "# 目标：把下面的句子改成on tap\n",
    "实践二运行前时间 = time.time()\n",
    "\n",
    "phrase = \"Don't panic!\"\n",
    "plist = list(phrase) # list(): --> list\n",
    "print(plist)\n",
    "new_phrase = ''.join(plist[1:3])+plist[5]+plist[4]+plist[7]+plist[6]\n",
    "print(plist)\n",
    "print(new_phrase)\n",
    "\n",
    "实践二运行后时间 = time.time()\n",
    "实践二代码的执行实践 = 实践二运行后时间-实践二运行前时间\n",
    "print(实践二代码的执行实践)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7069f719",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
